<?php

namespace app\admin\controller;

use app\common\BaseHelper as Helper;
use app\common\Common;

use app\admin\model\Image;
use app\admin\model\Featurecolumn;

use think\Request;

class Features extends Base
{
    public function _initialize() {
        parent::_initialize();

        $typeId = $this->request->param('typeId');
        $defaultWhere = $this->getDefaultWhere();
        $defaultWhere['typeId'] = $typeId;
        $this->defaultWhere = $defaultWhere;
        $this->searchFields = [
            'title' => [
                'label'     => '标题',
                'field'     => 'title',
                'type'      => 'text',
                'disabled'  => false,
                'condition' => 'like'
            ],
        ];

        $featurecolumnInfo = Featurecolumn::where(['id' => $typeId])->find();
        $this->assign('typeId', $typeId);
        $this->assign('typeName', $featurecolumnInfo['name']);
        $this->assign('area', $this->getArea($featurecolumnInfo));
        $this->assign('featurecolumnInfo', $featurecolumnInfo);
    }

    public function index(){

        return parent::index();
    }

    public function add(Request $request) {

        return parent::add($request);
    }

    public function addPost(Request $request, $redirect = '') {
        $redirect = url('admin/features/index', ['typeId' => $request->param('typeId')]);

        return parent::addPost($request, $redirect);
    }

    public function edit(Request $request) {

        return parent::edit($request);
    }

    public function editPost(Request $request, $redirect = ''){
        $redirect = url('admin/features/index', ['typeId' => $request->param('typeId')]);

        return parent::editPost($request, $redirect);
    }

    public function delete() {
        $model = model($this->model);

        $request = $this->request;
        $id = $request->param('id');
        if($id){
            // 删除一条
            $info = $model->find(['id', $id]);
            if(!$info){
                return $this->error('该特色内容不存在！');
            }
            //$result = $info->update(['status' => 90]);
            $result = $model->where('id', $id)->delete();

            if($result !== false){
                // Query执行后的操作
                $model->_after_delete($id);

                $logInfo = $this->admin->name . '删除了1条' . $this->model . '数据。';
                Common::adminLog($request, $logInfo);
            }
        }

        if($result !== false){
            return $this->success('删除成功！');
        }else{
            return $this->error('删除失败！');
        }
    }

    /**
     * 筛选条件
     */
    public function getFilterWhere($request){
        $param = $request->param();
        $where = [];
        if($param['search']['value']) {
            $filter = json_decode($param['search']['value'],true);
            if(isset($filter['cityId']) && $filter['cityId']){
                $where['cityId'] = $filter['cityId'];
            }
            if(isset($filter['xianId']) && $filter['xianId']){
                $where['xianId'] = $filter['xianId'];
            }
            if(isset($filter['townId']) && $filter['townId']){
                $where['townId'] = $filter['townId'];
            }
            if(isset($filter['villageId']) && $filter['villageId']){
                $where['villageId'] = $filter['villageId'];
            }
            if(isset($filter['typeId']) && $filter['typeId']){
                $where['typeId'] = $filter['typeId'];
            }
            if(isset($filter['title']) && $filter['title']){
                $where['title'] = ['like', '%'.$filter['title'].'%'];
            }
        }

        return $where;
    }

    public function detail(Request $request) {
        $model = model($this->model);
        $id = $request->param('id');
        // Get data info
        $info = $model->find($id);

        if(!$info){
            return $this->error('error !');
        }

        $info->organization = $this->getOrganization($info->createOper);

        return $this->view->fetch('detail', [
            'info' => $info
        ]);
    }

    /**
     * 获取 栏目
     */
    public function getType(){
        $param = $this->request->param();
        $model = model('Featurecolumn');

        $where = [];
        if(isset($param['cityId']) && $param['cityId']){
            $where['cityId'] = $param['cityId'];
        }
        if(isset($param['xianId']) && $param['xianId']){
            $where['xianId'] = $param['xianId'];
        }
        if(isset($param['townId']) && $param['townId']){
            $where['townId'] = $param['townId'];
        }
        if(isset($param['villageId']) && $param['villageId']){
            $where['villageId'] = $param['villageId'];
        }

        $html = '';
        $data = $model->where($where)->field('id, name')->select();

        if($data){
            foreach($data as $item){
                $html .= "<option value='{$item['id']}'>{$item['name']}</option>";
            }
        }

        return json($html);
    }

}
